from datetime import datetime
from database import db

from flask_login import UserMixin

# 用户模型
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password = db.Column(db.String(128), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    is_admin = db.Column(db.Boolean, default=False)
    issues = db.relationship('Issue', foreign_keys='Issue.assignee_id', backref='assignee', lazy=True)
    created_issues = db.relationship('Issue', foreign_keys='Issue.created_by', backref='creator', lazy=True)

# 标签模型
class Tag(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), unique=True, nullable=False)
    issues = db.relationship('Issue', secondary='issue_tag', backref=db.backref('tags', lazy=True))

# 里程碑模型
class Milestone(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    description = db.Column(db.Text)
    due_date = db.Column(db.DateTime)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    issues = db.relationship('Issue', backref='milestone', lazy=True)

# 问题标签关联表
issue_tag = db.Table('issue_tag',
    db.Column('issue_id', db.Integer, db.ForeignKey('issue.id'), primary_key=True),
    db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True)
)

# 问题模型
class Issue(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)
    description = db.Column(db.Text)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    status = db.Column(db.String(20), default='open')
    priority = db.Column(db.String(20), default='medium')
    due_date = db.Column(db.DateTime)
    created_by = db.Column(db.Integer, db.ForeignKey('user.id'))
    assignee_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    milestone_id = db.Column(db.Integer, db.ForeignKey('milestone.id'))
    comments = db.relationship('Comment', backref='issue', lazy=True)
    history = db.relationship('IssueHistory', backref='issue', lazy=True)
    files = db.relationship('File', backref='issue', lazy=True)
    visibility = db.Column(db.String(20), default='public')
    category = db.Column(db.String(50))

# 评论模型
class Comment(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.Text, nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    issue_id = db.Column(db.Integer, db.ForeignKey('issue.id'), nullable=False)
    # 添加与User模型的关系
    user = db.relationship('User', backref=db.backref('comments', lazy=True))
    files = db.relationship('File', backref='comment', lazy=True)

# 问题历史模型
class IssueHistory(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    issue_id = db.Column(db.Integer, db.ForeignKey('issue.id'), nullable=False)
    field_changed = db.Column(db.String(50), nullable=False)
    old_value = db.Column(db.Text)
    new_value = db.Column(db.Text)
    changed_by = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    changed_at = db.Column(db.DateTime, default=datetime.utcnow)

# 导入文件模型
from models.file import File